package eteam.aps.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import eteam.aps.api.U8Api;
import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Assert;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.dto.SaleOrderQueryDto;
import eteam.aps.model.dto.SalesOrderUpdateDto;
import eteam.aps.model.entity.InvCode;
import eteam.aps.model.entity.SalesOrder;
import eteam.aps.model.entity.other.Customer;
import eteam.aps.model.entity.other.CustomerOrder;
import eteam.aps.model.entity.other.SalesOrderHead;
import eteam.aps.model.entity.other.SalesOrderSave;
import eteam.aps.model.vo.sale.SaleOrderScanDto;
import eteam.aps.model.vo.sales_order.SalesOrderShow;
import eteam.aps.model.vo.sales_order.SalesOrderVo;
import eteam.aps.service.SalesOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 销售订单 控制层
 *
 * @author LiuLei
 */
@Api(tags = "销售订单")
@RestController
@RequestMapping("/soc")
public class SalesOrderController {

    @Autowired
    private SalesOrderService salesOrderService;

    @PostMapping("/gc")
    @ApiOperation("获取所有的客户信息")
    public R<List<Customer>> getCustomer() throws Exception {
        List<Customer> customer = U8Api.getCustomer();
        if (customer.isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(customer);
    }

    @PostMapping("/gcc")
    @ApiOperation("根据客户编码获取客户名称")
    @ApiImplicitParam(name = "customerCode", value = "客户编码")
    public R<Customer> getCustomerName(String customerCode) throws Exception {
        return R.success(U8Api.getCustomerByCode(customerCode));
    }

    @PostMapping("/gco")
    @ApiOperation("根据客户编码获取客户订单")
    @ApiImplicitParam(name = "customerCode", value = "客户编码")
    public R<List<CustomerOrder>> getCustomerOrder(String customerCode) throws Exception {
        List<CustomerOrder> salesOrderLis = U8Api.getSalesOrderLis(customerCode);
        if (salesOrderLis.isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(salesOrderLis);
    }

    @PostMapping("/gcboc")
    @ApiOperation("根据订单编号获取订单信息")
    @ApiImplicitParam(name = "orderCode", value = "订单编号")
    public R<SalesOrderSave> getCustomerByOrderCode(String orderCode) throws Exception {
        if (Helper.isEmpty(orderCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "订单编号不能为空！");
        return R.success(salesOrderService.getCustomerByOrderCode(orderCode));
    }

    @PostMapping("/gibc")
    @ApiOperation("根据物料编号查询物料信息")
    @ApiImplicitParam(name = "invCode", value = "物料编号")
    public R<List<InvCode>> getInvByInvCode(String invCode) throws JsonProcessingException {
        if (Helper.isEmpty(invCode))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "物料编号不能为空！");
        return R.success(U8Api.getInvByInvCode(invCode));
    }

    @PostMapping("/sco")
    @ApiOperation("保存订单信息")
    @ApiImplicitParam(name = "SalesOrderDto", value = "订单信息")
    public R<Integer> saveCustomerOrder(@RequestBody SalesOrderSave salesOrderSave) throws Exception {
        if (salesOrderSave == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        if (salesOrderSave.getSalesOrderBodyList() == null || salesOrderSave.getSalesOrderBodyList().isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "订单信息不能为空！");
        SalesOrderHead salesOrderHead = salesOrderSave.getSalesOrderHead();
        if (Helper.isEmpty(salesOrderHead.getfCustomerCode()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "客户编号不能为空！");
        if (Helper.isEmpty(salesOrderHead.getfCustomerName()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "客户名称不能为空！");
        if (Helper.isEmpty(salesOrderHead.getfSendAddress()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "送货地址不能为空！");
        if (salesOrderHead.getfSendDate() == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "发货日期不能为空！");
        if (Helper.isEmpty(salesOrderHead.getfSender()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "收货方不能为空！");
        return R.success(salesOrderService.saveCustomerOrder(salesOrderSave));
    }

    @PostMapping("/ssob/{fId}")
    @ApiOperation("根据fId查询订单信息")
    @ApiImplicitParam(name = "SalesOrderDto", value = "订单信息")
    public R<SalesOrderShow> selSalesOrderByFId(@PathVariable String fId) throws Exception {
        if (Helper.isEmpty(fId))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "内码不能为空！");
        return R.success(salesOrderService.selSalesOrderByFId(fId));
    }

    @PostMapping("/usob")
    @ApiOperation("根据fId修改订单信息")
    @ApiImplicitParam(name = "SalesOrderUpdateDto", value = "订单信息")
    public R<Integer> updateSalesOrderByFId(@RequestBody SalesOrderUpdateDto salesOrderUpdateDto) throws Exception {
        if (salesOrderUpdateDto == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        if (Helper.isEmpty(salesOrderUpdateDto.getfId()))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "内码不能为空！");
        return R.success(salesOrderService.updateSalesOrderByFId(salesOrderUpdateDto));
    }

    @PostMapping("/dsob/{fId}")
    @ApiOperation("根据fId删除订单信息")
    @ApiImplicitParam(name = "SalesOrderUpdateDto", value = "订单信息")
    public R<Integer> delBySalesOrderByFId(@PathVariable String fId) throws Exception {
        if (Helper.isEmpty(fId))
            throw new BusinessException(HttpStatus.BAD_REQUEST, "内码不能为空！");
        return R.success(salesOrderService.delBySalesOrderByFId(fId));
    }

    @PostMapping("/gso")
    @ApiOperation("条件查询订单信息")
    @ApiImplicitParam(name = "SaleOrderQueryDto", value = "订单查询条件")
    public R<JsonPage<SalesOrderVo>> getSalesOrderByQuery(@RequestBody SaleOrderQueryDto saleOrderQueryDto) throws Exception {
        if (saleOrderQueryDto == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(salesOrderService.getSalesOrderByQuery(saleOrderQueryDto));
    }

    @PostMapping("/sso")
    @ApiOperation("根据单号扫描订单信息")
    @ApiImplicitParam(name = "scan", value = "单号")
    public R<List<SalesOrder>> scanSalesOrder(String scan) throws Exception {
        if (scan == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "单号不能为空！");
        return R.success(salesOrderService.scanSalesOrder(scan));
    }

//    @PostMapping("/sp")
//    @ApiOperation("扫描产品信息")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "saleOrder", value = "订单编号"),
//            @ApiImplicitParam(name = "scan", value = "产品编号")
//    })
//    public R<MocoRdersSaleVo> scanProductNum(String saleOrder, String scan) throws Exception {
//        if (saleOrder == null)
//            throw new BusinessException(HttpStatus.BAD_REQUEST, "订单编号不能为空！");
//        if (scan == null)
//            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
//        return R.success(salesOrderService.scanProductNum(saleOrder, scan));
//    }


    @PostMapping("/sp")
    @ApiOperation("扫描产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "saleOrder", value = "订单编号"),
            @ApiImplicitParam(name = "scan", value = "产品编号")
    })
    public R<Void> scanProductNum(@RequestBody SaleOrderScanDto scan) throws Exception {
        Assert.isBlank(scan.getfSaleOrderCode(), "订单编号不能为空！");
        Assert.isTrue(scan.getfSeqNo() < 0, "请输入有效的行号！");
        Assert.isBlank(scan.getfBarCode(), "产品编号不能为空！");
        salesOrderService.scanProductNum(scan);
        return R.success();
    }

    @PostMapping("/upt")
    @ApiOperation("修改产品状态")
    @ApiImplicitParam(name = "bar", value = "产品编号")
    public R<Integer> updateProductNumType(String bar) throws Exception {
        if (bar == null)
            throw new BusinessException(HttpStatus.BAD_REQUEST, "产品编号不能为空！");
        return R.success(salesOrderService.updateProductNumType(bar));
    }

    @PostMapping("ad")
    @ApiOperation("新增一张发货单")
    @ApiImplicitParam(name = "map", value = "发货单信息")
    public R<String> addDelivery(@RequestBody Map<String, Object> map) throws Exception {
        if (map.isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(salesOrderService.addDelivery(map));
    }

    @PostMapping("as")
    @ApiOperation("审核一张发货单")
    @ApiImplicitParam(name = "map", value = "发货单信息")
    public R<Integer> auditDelivery(@RequestBody Map<String, Object> map) {
        if (map.isEmpty())
            throw new BusinessException(HttpStatus.BAD_REQUEST, "参数非法异常！");
        return R.success(U8Api.ptsDispatchAudit(map));
    }


}
